#include "common.h"
#include "console.h"
#include "commandcontroller.h"
#include "cppvi.h"
#include "document.h"

CommandController::CommandController(Console *console): Controller(console)
{
	
}

CommandController::~CommandController()
{

}

bool CommandController::processMessage(chtype message)
{
	Document *document = CppVi::getInstance()->getDocument();
	switch(message)
	{
	case keyi:
		this->activeConsole->setController(this->activeConsole->getInsertController());
		this->activeConsole->update();
		break;
	case keycolon:
		this->activeConsole->setController(this->activeConsole->getLineController());
		this->activeConsole->update();
		break;
	case keyd:
		document->del();
		this->activeConsole->update();
		break;
	case keyx:
		document->cut();
		this->activeConsole->update();
		break;
	case keyc:
		document->copy();
		break;
	case keyk:
		document->cutLine();
		this->activeConsole->update();
		break;
	case keyl:
		document->copyLine();
		break;
	case keyv:
		document->past();
		this->activeConsole->update();
		break;
	case keyenter:
		beep();
		break;
	case KEY_UP:
		document->moveOffset(-1, 0);
		this->activeConsole->update();
		break;
	case KEY_DOWN:
		document->moveOffset(1, 0);
		this->activeConsole->update();
		break;
	case KEY_BACKSPACE:
	case KEY_LEFT:
		document->moveOffset(0, -1);
		this->activeConsole->update();
		break;
	case KEY_RIGHT:
		document->moveOffset(0, 1);
		this->activeConsole->update();
		break;
		
	case KEY_NPAGE:
		document->keypagedown();
		this->activeConsole->update();
		break;
	case KEY_PPAGE:
		document->keypageup();
		this->activeConsole->update();
		break;
		
	case keydelete:
		document->keydelete();
		this->activeConsole->update();
		break;
	case KEY_HOME:
		document->keyhome();
		this->activeConsole->update();
		break;
	case KEY_END:
		document->keyend();
		this->activeConsole->update();
		break;
	default:
		beep();
		break;
	}
	return true;
}

int CommandController::getMode() const
{
	return commandMode;
}
